package parroty;
import java.util.HashSet;
import java.util.Set;

import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Blip;
import com.google.wave.api.Participants;
import com.google.wave.api.Wavelet;
import com.google.wave.api.event.GadgetStateChangedEvent;
import com.google.wave.api.event.WaveletParticipantsChangedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;

@SuppressWarnings("serial")
public class ParrotyServlet2 extends AbstractRobot {

//	private static String verificationToken = "AOijR2f0ifvwJZBITQyy39M2UUTmkPWB5JAYKP0Mj4V-tYWK0WmZe73QQ9SsMKpmuEah-R9dXIBguku2H454mKibRbGwlsDucC3vXEPwl_cGsTqHi2ZmTQg9Tf1WUFL7JL17Phi8ZDoeMnEL9PPu6Xf3HAUPPeNn8w==";
//	private static String securityToken = "319";
	
//	private static final String CONSUMER_KEY = "428891353130";
//	private static final String CONSUMER_SECRET = "10nmOIuD4ozpq9y6yIpeAOVH";
	
	public ParrotyServlet2() {
		// In a real environment, this should be removed
		// Verification token and security token shouldn't be exposed in the code
//		setupVerificationToken(verificationToken, securityToken);
		
//		setupOAuth(CONSUMER_KEY, CONSUMER_SECRET, "http://www-opensocial-sandbox.googleusercontent.com/api/rpc");
	}
	
	@Override
	protected String getRobotName() {
		return "Parroty the second";
	}

	@Override
	protected String getRobotAvatarUrl() {
		return "http://brc-app3.appspot.com/icon.jpg";
	}

	@Override
	protected String getRobotProfilePageUrl() {
		return "http://brc-app3.appspot.com/profile.html";
	}

	@Override
	public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
		Blip blip = event.getWavelet().reply("\nHowdy everybody!");
		
		Participants participants = event.getWavelet().getParticipants();
		Wavelet wave = newWave("wavesandbox.com", participants);
		wave.getRootBlip().append("\nI added a new wave.");
		
		Set<String> participants2 = new HashSet<String>();
		participants2.add("zarjay@wavesandbox.com");
		Wavelet wave2 = newWave("wavesandbox.com", participants2);
		wave2.getRootBlip().append("\nI added another wave.");
	}

	@Override
	public void onWaveletParticipantsChanged(WaveletParticipantsChangedEvent event) {
		log("Participants changed.");
		for (String newParticipant: event.getParticipantsAdded()) {
			Blip blip = event.getWavelet().reply("\nHowdy : " + newParticipant);
		}
	}

	// TODO: Copy text from one wave to another
	// Note: Need a way for admins to easily configure a robot.
	// Idea: Provide separate section in robot (web app) for admins to log in and configure.

	private void addText1(Blip blip) {
		String text1 = "1. Ping portal.insidebrady.com You should get a response that looks like the following: [insert screenshot here]. Insert the SS here:";
		String text2 = "2. Is this affecting all users / locations.";
		String text3 = "3. Call Luke @ x12-3456.";

		int start = blip.length();

		blip.append("\n" + text1);
		blip.append("\n" + text2);
		blip.append("\n" + text3);

		int end = blip.length();

		blip.range(start, end).annotate("style/fontFamily", "Times New Roman");
	}

	private void addText2(Blip blip) {
		String htmlText = "<ol><li>Ping portal.insidebrady.com You should get a response that looks like the following: <img src=\"http://www.gomonews.com/wp-content/uploads/2009/08/question-mark.jpg\" />. Insert the SS here:</li>"
			+ "<li>Is this affecting all users / locations.</li>"
			+ "<li>Call Luke @ x12-3456.</li></ol>";
		blip.appendMarkup(htmlText);
	}

	private void copyWave(Blip blip) {
		blip.getContent();

	}


	@Override
	public void onGadgetStateChanged(GadgetStateChangedEvent event) {
		Blip blip = event.getWavelet().getRootBlip();
		blip.append("\nThe gadget state changed.");
		addText2(blip);
	}

}